<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="navTop" title="登录">
      <van-icon
        class="closeIcon"
        slot="left"
        name="arrow-left"
        @click="$router.back()"
      />
    </van-nav-bar>
    <!-- 导航栏 -->
    <!-- 登陆表单 -->
    <van-form @submit="onSubmit" ref="userInfoRef">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        type="number"
        maxlength="11"
        :rules="userInfoRules.mobile"
      >
        <i slot="left-icon" class="xcc xccshouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        type="number"
        :rules="userInfoRules.code"
      >
        <i slot="left-icon" class="xcc xccyanzhengma1"></i>
        <template #button>
          <van-count-down
            v-if="isCountDownShow"
            :time="1000 * 3"
            format="ss s"
            @finish="isCountDownShow = false"
            slot="button"
          />
          <van-button
            v-else
            class="numBtn"
            native-type="button"
            slot="button"
            round
            size="small"
            type="default"
            @click="sendNumBtn"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login_btn_container">
        <van-button class="login_btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <!-- 登陆表单 -->
  </div>
</template>
<script>
import { login, sendMsg } from '@/api/user'
export default {
  name: 'loginPages',
  data() {
    return {
      user: {
        mobile: '', // 手机号
        code: '' // 验证码
      },
      userInfoRules: {
        mobile: [
          { required: true, message: '请输入手机号！' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号格式填写错误！' }
        ],
        code: [
          { required: true, message: '请输入验证码！' },
          { pattern: /^\d{6}$/, message: '验证码格式填写错误！' }
        ]
      },
      isCountDownShow: false
    }
  },
  methods: {
    async onSubmit() {
      this.$toast.loading({
        duration: 1000, // 持续时间 0表示持续展示不停止
        forbidClick: true, // 是否禁止背景点击
        message: '正在登录...' // 提示消息
      })
      // 获取表单数据
      const user = this.user
      // 表单验证

      // 提交表单发起请求
      try {
        const { data } = await login(user)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功！')
        // 登录成功 跳转页面
        this.$router.back()
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail('手机号或验证码错误！')
        } else {
          this.$toast.fail('登陆失败，请稍后再试')
        }
      }
      // 根据请求响应结果处理后续操作
    },
    async sendNumBtn() {
      // 校验手机号 validate方法会返回promise对象
      try {
        // 验证成功
        await this.$refs.userInfoRef.validate('mobile')
      } catch (err) {
        // 验证失败
        this.$toast.fail(err)
      }
      // 验证成功 显示倒计时
      this.isCountDownShow = true
      // 请求发送验证码
      try {
        await sendMsg(this.user.mobile)
        this.$toast.success('发送成功！')
      } catch (err) {
        if (err.response.status === 429) {
          this.$toast.fail('操作过于频繁，请稍后再试！')
        } else {
          this.$toast.fail('手机号格式输入错误！')
        }
      }
    }
  }
}
</script>
<style scoped lang="less">
.login-container {
  .xcc {
    font-size: 37px;
  }
  .numBtn {
    background-color: #ededed;
    width: 152px;
    height: 46px;
    line-height: 46px;
    font-size: 22px;
    color: #666;
  }
  .login_btn_container {
    padding: 53px 33px;
    .login_btn {
      background-color: #6db4fb;
      border: none;
    }
  }
  .closeIcon {
    color: #fff;
  }
}
</style>
